<?php
/**
* Optional: set 'ot_show_pages' filter to __return_false or __return_true. false will hide and true will display settings & documentation page
*/
add_filter( 'ot_show_pages', '__return_false' );


// Remove Option Tree Settings Menu
function remove_ot_menu(){
	remove_menu_page( 'ot-settings' );	
}
add_action( 'admin_menu', 'remove_ot_menu' );

/**
* Theme Options
*/
include_once( 'theme-options.php' );



/*-----------------------------------------------------------------------------------*/
/*	Include Shortcodes Files
/*-----------------------------------------------------------------------------------*/	
	require_once ('functions/shortcodes.php');


/*-----------------------------------------------------------------------------------*/
/*	Theme Options
/*-----------------------------------------------------------------------------------*/	
	if ( function_exists( 'ot_get_option' ) ) 
	{
		$theme_options = get_option('option_tree');
	}


/*-----------------------------------------------------------------------------------*/
/*	Constants
/*-----------------------------------------------------------------------------------*/	
	define('LOCALITY_FUNCTIONS', get_template_directory() . '/functions/');
	define('LOCALITY_WIDGETS', get_template_directory() . '/widgets/');


/*-----------------------------------------------------------------------------------*/
/*	Custom Widgets
/*-----------------------------------------------------------------------------------*/	
	require_once (LOCALITY_WIDGETS . 'featured-property.php');
	require_once (LOCALITY_WIDGETS . 'locality-agent.php');
	require_once (LOCALITY_WIDGETS . 'search-widget.php');
	require_once (LOCALITY_WIDGETS . 'twitter-widget.php');
	
	add_action( 'widgets_init', 'locality_load_widgets' ); 
	 
	function locality_load_widgets() 
	{
		register_widget( 'Locality_Featured_Property' );
		register_widget( 'Locality_Agent' );
		register_widget( 'Search_Widget' );
		register_widget( 'Twitter_Widget' );
	}
	
	
/*-----------------------------------------------------------------------------------*/
/*	Include Custom Post Type For Property
/*-----------------------------------------------------------------------------------*/	
	require_once (LOCALITY_FUNCTIONS . 'property-post-type.php');


/*-----------------------------------------------------------------------------------*/
/*	Load Text Domain
/*-----------------------------------------------------------------------------------*/	
	load_theme_textdomain( 'locality' );


/*-----------------------------------------------------------------------------------*/
/*	Get Currency
/*-----------------------------------------------------------------------------------*/	
	if ( function_exists('register_sidebar') )	
	{			
	
		register_sidebar(array('name'=>'Sidebar Home',
			'before_widget' => '<div class="side-widget">',
			'after_widget' => '</div>',
			'before_title' => '<h3>',
			'after_title' => '</h3>'
		));
	
		register_sidebar(array('name'=>'Sidebar Blog',
			'before_widget' => '<div class="side-widget">',
			'after_widget' => '</div>',
			'before_title' => '<h3>',
			'after_title' => '</h3>'
		));
		
		register_sidebar(array('name'=>'Sidebar',
			'before_widget' => '<div class="side-widget">',
			'after_widget' => '</div>',
			'before_title' => '<h3>',
			'after_title' => '</h3>'
		));
		
		register_sidebar(array('name'=>'Footer Col1',
			'before_widget' => '<div class="footer-widget">',
			'after_widget' => '</div>',
			'before_title' => '<h3>',
			'after_title' => '</h3>'
		));
	
		register_sidebar(array('name'=>'Footer Col2',
			'before_widget' => '<div class="footer-widget">',
			'after_widget' => '</div>',
			'before_title' => '<h3>',
			'after_title' => '</h3>'
		));
	
		register_sidebar(array('name'=>'Footer Col3',
			'before_widget' => '<div class="footer-widget">',
			'after_widget' => '</div>',
			'before_title' => '<h3>',
			'after_title' => '</h3>'
		));
	}
	
	
/*-----------------------------------------------------------------------------------*/
/*	Configure Thumbnails
/*-----------------------------------------------------------------------------------*/	
	if( function_exists( 'add_theme_support' ) ) 
	{
			add_theme_support( 'post-thumbnails' );
			add_image_size( 'blog-post-thumb', 454, 332, true); // for blog pages
			add_image_size( 'property-big-post-thumb', 666, 358, true); // for slider
			add_image_size( 'property-post-thumb', 296, 176, true); // for Property listing page
			add_image_size( 'property-gallery', 406, 300, true); // for sloider thumb
			add_image_size( 'full-size', '', '', false);
	}	  	  


/*-----------------------------------------------------------------------------------*/
/*	Meta Boxes
/*-----------------------------------------------------------------------------------*/		  
	define( 'RWMB_URL', trailingslashit( get_template_directory_uri().'/meta-box' ) );
	define( 'RWMB_DIR', trailingslashit( get_template_directory().'/meta-box' ) );	
	require_once RWMB_DIR.'meta-box.php';		
	include get_template_directory().'/meta-box-config.php';      
	

/*-----------------------------------------------------------------------------------*/
/*	Custom Menu Support
/*-----------------------------------------------------------------------------------*/	
	  add_theme_support( 'menus' );
	  if ( function_exists( 'register_nav_menus' ) ) 
	  {
			register_nav_menus(
				array(
				  'header-menu' => 'Header Menu',
				  'footer-menu' => 'Footer Menu'
				)
			);
	  }	
		

/*-----------------------------------------------------------------------------------*/
/*	Custom Excerpt Length
/*-----------------------------------------------------------------------------------*/	
	  function locality_excerpt($len=80, $trim="&hellip;")
	  	{
		  	$limit = $len+1;
		  	$excerpt = explode(' ', get_the_excerpt(), $limit);
		  	$num_words = count($excerpt);
		  	if($num_words >= $len)
			{
		  		$last_item=array_pop($excerpt);
		  	}
		  	else
		  	{
		  		$trim="";
		  	}
		  	$excerpt = implode(" ",$excerpt)."$trim";
		  	
		  	echo $excerpt;
	  	}
	  	
	  	function get_locality_excerpt($len=80, $trim="&hellip;")
	  	{
	  		$limit = $len+1;
	  		$excerpt = explode(' ', get_the_excerpt(), $limit);
	  		$num_words = count($excerpt);
		  	if($num_words >= $len){
		  		$last_item=array_pop($excerpt);
		  	}
		  	else
		  	{
		  		$trim="";
		  	}
	  		$excerpt = implode(" ",$excerpt)."$trim";
	  
	  		return $excerpt;
	  	}



/*-----------------------------------------------------------------------------------*/
/*	Theme Pagination Method
/*-----------------------------------------------------------------------------------*/	
	function theme_pagination($pages = '')
	{
		global $paged;
		
		if(is_page_template('template-home.php'))
		{
			$paged = intval(get_query_var( 'page' ));
		}
		
		if(empty($paged))$paged = 1;
						
		if($pages == '')
		{	
			global $wp_query;
			$pages = $wp_query->max_num_pages;
			if(!$pages)
			{
				$pages = 1;
			}
		}
		
		
		if(1 != $pages)
		{
			echo "<div id='pagination'>";				
			for ($i=1; $i <= $pages; $i++)
			{
				if (1 != $pages )
				{
					echo ($paged == $i)? "<a href='".get_pagenum_link($i)."' class='btn current'>".$i."</a> ":"<a href='".get_pagenum_link($i)."' class='btn'>".$i."</a> "; 
				}
			}						
			echo "</div>";
		}
	}


/*-----------------------------------------------------------------------------------*/
/*	Theme Custom Comments
/*-----------------------------------------------------------------------------------*/	
    function mytheme_comment($comment, $args, $depth) 
	{
       $GLOBALS['comment'] = $comment; 
       ?>
    	<li class="comment-conatiner">
    		<div class="avatar-wrapper">
				<a href="<?php echo get_comment_author_url(); ?>">
    				<?php echo get_avatar($comment->comment_author_email,65); ?>
				</a>
    		</div>
    		<div class="comment">
    			<h5 class="comment-meta">posted by <?php comment_author_link(); ?> on <span><?php printf(('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></span></h5>
    			<?php comment_text() ?> 
    		</div>
    		<p class="reply-btn" href="#"><?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?></p>
    	</li>
       <?php
    }


/*-----------------------------------------------------------------------------------*/
/*	For Loading Required JS Files
/*-----------------------------------------------------------------------------------*/	
	if(!function_exists('load_theme_scripts'))
	{
		function load_theme_scripts()
		{		
			if (!is_admin()) {
				
				$jscriptURL = get_template_directory_uri().'/js/';
				
				// Registering New Scripts						
				wp_register_script('validate', $jscriptURL.'jquery.validate.js','jquery');
				wp_register_script('forms', $jscriptURL.'jquery.form.js','jquery');									
				wp_register_script('easing', ($jscriptURL.'jquery.easing.1.3.js'), 'jquery');
				wp_register_script('cycle', $jscriptURL.'jquery.cycle.all.js', 'jquery');
				wp_register_script('backgroundPosition', $jscriptURL.'jquery.backgroundPosition.js', 'jquery');
				wp_register_script('selectbox', $jscriptURL.'jquery.selectbox-0.5.js', 'jquery');
				wp_register_script('EqualHeightsPlugin', $jscriptURL.'equalheights.js', 'jquery');
				wp_register_script('custom_script',$jscriptURL.'script.js', 'jquery','1.0',true);
				wp_register_script('prettyphoto',$jscriptURL.'prettyPhoto/js/jquery.prettyPhoto.js', 'jquery');
				
				// Enqueing Scripts
				wp_enqueue_script('jquery');
				wp_enqueue_script('easing');
				wp_enqueue_script('backgroundPosition');
				wp_enqueue_script('selectbox');
				wp_enqueue_script('jquery-ui-core');
				wp_enqueue_script('jquery-ui-widget');
				wp_enqueue_script('jquery-ui-mouse');
				wp_enqueue_script('jquery-ui-slider');
				wp_enqueue_script('EqualHeightsPlugin');
				wp_enqueue_script('prettyphoto');
				
				// Loading Cycle Plugin for only home page and single property page
				if ( is_page_template('template-home.php') || is_singular('property') )
				{
					wp_enqueue_script('cycle');
				}				
				
				// Loading Forms and Validation Script for Contact Pate Template only
        		if ( is_page_template('template-contact.php') )
				{ 
					wp_enqueue_script('forms');				
					wp_enqueue_script('validate');
				}
				
				// Loading Custom Script which calls to all other scripts methods
				wp_enqueue_script('custom_script');
				
			}
		}
	}
		
	add_action('wp_enqueue_scripts', 'load_theme_scripts');	


/*-----------------------------------------------------------------------------------*/
/*	For Loading Required CSS Files
/*-----------------------------------------------------------------------------------*/	

	if(!function_exists('load_theme_styles'))
	{
		function load_theme_styles()
		{		
			if (!is_admin()) 
			{
				
				wp_register_style('jquery-ui-style',  get_template_directory_uri() . '/smoothness/jquery-ui-1.8.18.custom.css');
				wp_register_style('prettyPhoto',  get_template_directory_uri() . '/js/prettyPhoto/css/prettyPhoto.css');

				// Enque Styles	
				wp_enqueue_style('jquery-ui-style');
				wp_enqueue_style('prettyPhoto');	
								
			}		
		}
	}
	
	add_action('wp_enqueue_scripts', 'load_theme_styles');


/*-----------------------------------------------------------------------------------*/
/*	Contact Form Ajax Handler
/*-----------------------------------------------------------------------------------*/	

add_action( 'wp_ajax_nopriv_send_message', 'send_message' );
add_action( 'wp_ajax_send_message', 'send_message' );	
	
	function send_message()
	{	
		if(isset($_POST['email'])):
		
			$name = $_POST['name'];
            $email = $_POST['email'];
            $message = $_POST['message'];
			$address = $_POST['target'];
			
            if(get_magic_quotes_gpc()) {
                    $message = stripslashes($message);
            }

             $e_subject = __('You have Received a Message From ','locality') . $name . '.';

             $e_body = __('You have Received a Message From ','locality') . $name . __(", their additional message is as follows. ",'locality')." \r\n\n ";

             $e_content = "\" $message \"\r\n\n";

             $e_reply = __('You can contact ','locality') . $name . __(' via email, ','locality') . $email;

             $msg = $e_body . $e_content . $e_reply;

             if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n","-f $address"))
			 {
    		 	_e('Message Sent Successfully!','locality');
			 }
			 else
			 {
				 _e('Server Error: Mailing Method Failed!','locality');
			 }
			 
		else:
			
				_e('Message Sending Failed!','locality');
		
		endif;
		
		die;
	}
	
	
/*-----------------------------------------------------------------------------------*/
/*	Add Mobile, Office and Fax Numbers to User Profile.
/*-----------------------------------------------------------------------------------*/	
	add_filter('user_contactmethods', 'my_user_contactmethods');
	
	function my_user_contactmethods($user_contactmethods)
	{
		$user_contactmethods['mobile'] = 'Mobile';
		$user_contactmethods['office'] = 'Office';
		$user_contactmethods['fax'] = 'Fax';
		
		return $user_contactmethods;
	}
	

/*-----------------------------------------------------------------------------------*/
/*	Get Currency
/*-----------------------------------------------------------------------------------*/	
	function theme_currency()
	{
		if ( function_exists( 'ot_get_option' ) ) 
		{
						
			$currency = ot_get_option( 'currency_sign' );
			if(!empty($currency))
			{
				echo $currency;
			}
			else
			{
				_e('$','locality');
			}
		}
		else
		{
			_e('$','locality');
		}
	}
	

/*-----------------------------------------------------------------------------------*/
/*	Get Currency
/*-----------------------------------------------------------------------------------*/	
	function theme_unit()
	{
		if ( function_exists( 'ot_get_option' ) ) 
		{
						
			$unit = ot_get_option( 'unit' );
			if(!empty($unit))
			{
				echo ' '.$unit;
			}
			else
			{
				_e(' Sq Ft','locality');
			}
		}
		else
		{
			_e(' Sq Ft','locality');
		}
	}

/*-----------------------------------------------------------------------------------*/
/*	Feed Links
/*-----------------------------------------------------------------------------------*/	
	add_theme_support( 'automatic-feed-links' );


/*-----------------------------------------------------------------------------------*/
/*	Add Custom Background
/*-----------------------------------------------------------------------------------*/
	add_theme_support( 'custom-background' );
